﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using com.bm.facade.OrderService;


namespace com.bm.facade
{
    public class OperationFacade : IOperationFacade
    {

        public OrderService.Order CreateAndCalcQuote(String name, String email, String[] parts, baySize size)
        {
            int price;
            int repair_time;
            int order_id;

            Warehouse.WarehouseServiceClient wh_proxy = new Warehouse.WarehouseServiceClient();
            OrderService.OrderServiceClient o_proxy = new OrderService.OrderServiceClient();

            // Get price of parts
            price = wh_proxy.CalculateOrderPrice(parts);

            // Get repair time
            repair_time = wh_proxy.CalculateRepairTime(parts);

            // Generate quote
            order_id = o_proxy.generateQuote(name, email, parts, price, size.ToString(), repair_time);

            // Return order
            return o_proxy.getOrderByID(order_id);
            

        }

        public OrderService.Order UpdateOrderAndReservePartsBay(OrderService.Order order)
        {
            
            Warehouse.WarehouseServiceClient wh_proxy = new Warehouse.WarehouseServiceClient();
            OrderService.OrderServiceClient o_proxy = new OrderService.OrderServiceClient();
            Reseller.ResellerServiceClient r_proxy = new Reseller.ResellerServiceClient();
            Garage.GarageServiceClient g_proxy = new Garage.GarageServiceClient();
            Garage.ServicebayRequest request = new Garage.ServicebayRequest();
            Garage.ServicebayResponse response = new Garage.ServicebayResponse();

            // Set status to order
            o_proxy.setStatus(order.id, "Order");
            
            // Reserve parts
            String[] temp;
            temp = order.parts;

            foreach (String part in temp) {

                // Get current stock
                int in_stock = wh_proxy.checkStockPart(part);

                // Order more parts from reseller if parts quantity is less than one
                if (in_stock < 2)
                {

                    r_proxy.deliverParts(part, 5);
                }

                // Deduct part from warehouse
                wh_proxy.setQuantityPart(part, (in_stock - 1));
            }

            // Generate request to garage
            request.Order_ID = order.id;
            request.numberOfDays = "1"; // All repairs takes one day
            request.size = order.size;

            // Book bay and mechanic && insert start-end date
            response = g_proxy.bookServiceBay(request);
            
            // Update order with start-, end date
            o_proxy.setStartEnd(order.id, response.startDate, response.endDate);
            
            return o_proxy.getOrderByID(order.id);
        }


        public Order getOrderByID(int id)
        {
            OrderService.OrderServiceClient proxy = new OrderServiceClient();

            return proxy.getOrderByID(id);
        }


        public Order rejectQuote(int id)
        {

            OrderService.OrderServiceClient o_proxy = new OrderService.OrderServiceClient();

            o_proxy.setStatus(id, "Declined");

            return o_proxy.getOrderByID(id);
           
        }


        public Order cancelOrderByID(int id)
        {
            Warehouse.WarehouseServiceClient wh_proxy = new Warehouse.WarehouseServiceClient();
            OrderService.OrderServiceClient o_proxy = new OrderService.OrderServiceClient();
            Garage.GarageServiceClient g_proxy = new Garage.GarageServiceClient();

            OrderService.Order temp = o_proxy.getOrderByID(id);

            // Set order status to cancelled
            o_proxy.setStatus(id, "Cancelled");

            // Cancel service bay
            g_proxy.cancelServiceBay(temp.id);

            // Replenish stock / unreserve parts
            wh_proxy.replenishStockOnCancel(temp.parts);


            return o_proxy.getOrderByID(id);
        }
    }

}

